package com.zry.fertilizer.view

import android.app.DatePickerDialog
import android.content.Context
import java.util.*

class DataTimeUtil(context: Context) {
    private var calendar: Calendar? = null
    private var dateYear: String? = null
    private var dateMonth: String? = null
    private var dateDay: String? = null


    init {
        calendar = Calendar.getInstance()
        val datePickerDialog = DatePickerDialog(context, DatePickerDialog.THEME_HOLO_LIGHT, { //年月日
            view, year, monthOfYear, dayOfMonth ->
            dateYear = year.toString()

            dateMonth = if ((monthOfYear + 1).toString().length == 1) {
                "0" + (monthOfYear + 1).toString() + ""
            } else {
                (monthOfYear + 1).toString()
            }
            dateDay = if (dayOfMonth.toString().length == 1) {
                "0" + dayOfMonth.toString() + ""
            } else {
                dayOfMonth.toString()
            }

            if (null != dateYear && null != dateMonth) {


                if (listener != null) {
                    listener!!.handleResult(dateYear!!, dateMonth!!, dateDay!!)
                }
            }
        }, calendar!!.get(Calendar.YEAR), calendar!!.get(Calendar.MONTH), calendar!!.get(Calendar.DAY_OF_MONTH))
        datePickerDialog.show()
    }


    private var listener: DateListener? = null

    interface DateListener {
        fun handleResult(dateYear: String, dateMonth: String, dateDay: String)
    }

    fun setListener(listener: DateListener): DataTimeUtil {
        this.listener = listener
        return this
    }

}